package com.wdh.kjs.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wdh.kjs.R;
import com.wdh.kjs.activity.CompletedTransferActivity;
import com.wdh.kjs.activity.RecodeActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class IM_investrecord_Adapter extends BaseAdapter {
    private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    private Context context;
    private HashMap<String, String> maps = new HashMap<String, String>();
    private int periods = 0;

    public IM_investrecord_Adapter(Context context, List<HashMap<String, String>> data) {
        this.data = data;
        this.context = context;
    }

    public void resetData(List<HashMap<String, String>> data) {
        this.data.clear();
        this.data.addAll(data);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return data.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            convertView = View.inflate(context, R.layout.fragment_invest_record, null);

//            viewHolder.investrecord_type = (TextView) convertView
//                    .findViewById(R.id.investrecord_type);
            viewHolder.investrecord_name = (TextView) convertView
                    .findViewById(R.id.investrecord_name);
            viewHolder.investrecord_annualRate = (TextView) convertView
                    .findViewById(R.id.investrecord_annualRate);
            viewHolder.investrecord_projectTime = (TextView) convertView
                    .findViewById(R.id.investrecord_projectTime);
            viewHolder.investrecord_repayType = (TextView) convertView
                    .findViewById(R.id.investrecord_repayType);
            viewHolder.investrecord_investMoney = (TextView) convertView
                    .findViewById(R.id.investrecord_investMoney);
            viewHolder.investrecord_state = (TextView) convertView
                    .findViewById(R.id.investrecord_state);
            viewHolder.investrecord_orderNum = (TextView) convertView
                    .findViewById(R.id.investrecord_orderNum);
//            viewHolder.investrecord_date = (TextView) convertView
//                    .findViewById(R.id.investrecord_date);
            viewHolder.investrecord_time = (TextView) convertView
                    .findViewById(R.id.investrecord_time);
            viewHolder.completedTransfer = (Button) convertView
                    .findViewById(R.id.completedTransfer);
            viewHolder.investrecord_LinearLayout = (LinearLayout) convertView.findViewById(R.id.investrecord_LinearLayout);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        maps = data.get(position);
//        viewHolder.investrecord_type.setText(maps.get("investrecord_type"));
        viewHolder.investrecord_name.setText(maps.get("investrecord_name"));
        viewHolder.investrecord_annualRate.setText(maps.get("investrecord_annualRate"));
        viewHolder.investrecord_projectTime.setText(maps.get("investrecord_projectTime"));
        viewHolder.investrecord_repayType.setText(maps.get("investrecord_repayType"));
        viewHolder.investrecord_investMoney.setText(maps.get("investrecord_investMoney"));
        viewHolder.investrecord_state.setText(maps.get("investrecord_state"));
        viewHolder.investrecord_orderNum.setText(maps.get("investrecord_orderNum"));
//        viewHolder.investrecord_date.setText(maps.get("investrecord_date"));
        viewHolder.investrecord_time.setText(maps.get("investrecord_time"));

        if(maps.get("investrecord_state").equals("待提供支付凭证")){
            viewHolder.completedTransfer.setVisibility(View.VISIBLE);
            ItemListener itemListener = new ItemListener(position);
            viewHolder.completedTransfer.setOnClickListener(itemListener);
        }else {
            viewHolder.completedTransfer.setVisibility(View.GONE);
        }
        if (maps.get("investrecord_state").equals("未支付")){
            UnpaudItemListener unpaiditemListener = new UnpaudItemListener(position);
            viewHolder.investrecord_LinearLayout.setOnClickListener(unpaiditemListener);
        }
        return convertView;
    }
    private class UnpaudItemListener implements View.OnClickListener {

        private int m_position;

        public UnpaudItemListener(int position) {
            this.m_position = position;
        }

        @Override
        public void onClick(View v) {
            Intent it = new Intent(context, RecodeActivity.class);
            it.putExtra("orderid", data.get(m_position).get("investrecord_orderNum"));
            it.putExtra("itemid", data.get(m_position).get("investrecord_itemId"));
            it.putExtra("money", data.get(m_position).get("money"));
            context.startActivity(it);
        }
    }
    private class ItemListener implements View.OnClickListener {

        private int m_position;

        public ItemListener(int position) {
            this.m_position = position;
        }

        @Override
        public void onClick(View v) {
            Intent it = new Intent(context, CompletedTransferActivity.class);
                    it.putExtra("id", data.get(m_position).get("Id"));
                    context.startActivity(it);
        }
    }

        private class ViewHolder {
//        TextView investrecord_type;
        TextView investrecord_name;
        TextView investrecord_annualRate;
        TextView investrecord_projectTime;
        TextView investrecord_repayType;
        TextView investrecord_orderNum;
        TextView investrecord_state;
        TextView investrecord_investMoney;
        TextView investrecord_time;
//        TextView investrecord_date;
        Button completedTransfer;
LinearLayout investrecord_LinearLayout;
    }


}
